package com.ffapp.rainmonitorcustom.alarm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisActivity;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.AlarmListData;
import com.ffapp.rainmonitorcustom.bean.AlarmProcessmanListData;
import com.ffapp.rainmonitorcustom.bean.BaseListData;
import com.ffapp.rainmonitorcustom.bean.BaseListDataListBean;
import com.ffapp.rainmonitorcustom.bean.BasisBean;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.DialogUtils;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * 告警详情处理
 * */
public class AlarmDetailProcessActivity extends BasisActivity {


    AlarmListData mDetailBean;
    BaseListDataListBean listWaysBean;
    BaseListData currentDataWay;

    @BindView(R.id.recyclerview_ways)
    RecyclerView recyclerview_ways;
    @BindView(R.id.edit_otherway)
    EditText edit_otherway;
    @BindView(R.id.text_process_man)
    TextView text_process_man;
    @BindView(R.id.view_process)
    View view_process;

    String chooseIds;

    ArrayList<AlarmProcessmanListData> listProcessMan = new ArrayList<>();

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.alarm_detail_process_activity);

        GridLayoutManager layoutManager = new GridLayoutManager(mContext,2);
        recyclerview_ways.setLayoutManager(layoutManager);

        setTitle("告警处理");
        setTitleLeftButton(null);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        mDetailBean = (AlarmListData) getIntent().getExtras().getSerializable("data");

        listWaysBean = new BaseListDataListBean();
        BaseListData data;
        data = new BaseListData("1","误报");
        listWaysBean.getList().add(data);
        data = new BaseListData("2","故障");
        listWaysBean.getList().add(data);
        data = new BaseListData("3","火警");
        listWaysBean.getList().add(data);
        data = new BaseListData("4","屏蔽预警");
        listWaysBean.getList().add(data);
        data = new BaseListData("5","异常");
        listWaysBean.getList().add(data);
        data = new BaseListData("6","其他");
        listWaysBean.getList().add(data);
        recyclerview_ways.setAdapter(new MyAdapterList());

        setProcessMan();
    }


    @OnClick(R.id.view_process_man)
    void processmanChoose(){
         Bundle extras = new Bundle();
         extras.putSerializable("data",mDetailBean);
         extras.putSerializable("list",listProcessMan);
        ActivityTool.skipActivityForResult(mContext,AlarmlPMChooseActivity.class,extras,0);
    }


    void setProcessMan(){

        if(listProcessMan == null || listProcessMan.size() == 0){
            text_process_man.setText("");
            chooseIds = "";
            return;
        }
        StringBuilder builder = new StringBuilder();
        StringBuilder builderId = new StringBuilder();
        for(AlarmProcessmanListData data:listProcessMan){
            builder.append(data.getCOL_NAME()).append(",");
            builderId.append(data.getCOL_ID()).append(",");
        }
        builder.deleteCharAt(builder.length()-1);
        builderId.deleteCharAt(builder.length()-1);
        chooseIds =builderId.toString();
        text_process_man.setText(builder.toString());

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        listProcessMan = (ArrayList<AlarmProcessmanListData>) data.getSerializableExtra("list");
        setProcessMan();
    }

    @OnClick(R.id.btn_ok)
    void post(){
        String other = edit_otherway.getText().toString().trim();

        if(currentDataWay == null){
            showToast("请选择处理状态");
            return;
        }


        if(currentDataWay.getId().equals("3") && TextUtils.isEmpty(chooseIds)){
            showToast("请选择接警人");
            return;
        }


        RequestParams params = new RequestParams();
        showProgress();
        params.put("deviceType", mDetailBean.getDEVICETYPE());
        params.put("no", mDetailBean.getNO());
        params.put("id", mDetailBean.getID());
        params.put("explain", other);
        params.put("status", currentDataWay.getId());
        params.put("offcomNo", mDetailBean.getOFFCOMNO());

        params.put("userids", chooseIds);


        HttpRestClient.post(Constants.URL_ALARM_POST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        setResult(Activity.RESULT_OK);
                        DialogUtils.DialogOKmsgFinish(mContext,((BasisBean)result).getResultData());
                    }


                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                    }
                },
                0, BasisBean.class);
    }

    public class MyAdapterList extends XRecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.alarm_handle_grid_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            BaseListData data = listWaysBean.getList().get(position);
            viewHolder.text_name.setText(data.getName());
            if(currentDataWay != null && data.getName().equals(currentDataWay.getName())){
                viewHolder.list_item.setSelected(true);
            }else{
                viewHolder.list_item.setSelected(false);
            }
            viewHolder.list_item.setTag(data);
        }


        //获取数据的数量
        @Override
        public int getItemCount() {
            return listWaysBean == null ? 0 : listWaysBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                currentDataWay = (BaseListData) v.getTag();
                notifyDataSetChanged();
                onTypeChoose();
//                currentDataWay.setSeleted();
            }


        }
    }

    void onTypeChoose(){
        if(currentDataWay == null)return;
        if(currentDataWay.getId().equals("3")){//火警
            view_process.setVisibility(View.VISIBLE);
        }else{
            view_process.setVisibility(View.GONE);
            listProcessMan.clear();
            setProcessMan();
        }
    }

}
